#ifndef SGP30_ESWIN_H
#define SGP30_ESWIN_H

#include "../board/sdk_project_config.h"

#include "i2c_driver.h"
#include "status.h"

// SGP30 I2C 地址 (7位地址)
// #define SGP30_I2C_ADDRESS 0x58

// 状态定义
typedef enum {
    SGP30_SUCCESS         = STATUS_SUCCESS,
    SGP30_ERR_I2C_TIMEOUT = STATUS_I2C_RECEIVED_NACK,
    SGP30_ERR_BAD_CRC,
    SGP30_SELF_TEST_FAIL,
    SGP30_ERR_INIT_FAILED
} SGP30_Status;

// SGP30 数据结构
typedef struct {
    uint8_t i2cInstance;          // I2C 实例 (如 INST_I2C_0)
    i2c_master_state_t *i2cState; // I2C 状态指针

    // 传感器数据
    uint16_t CO2;               // CO₂ 浓度 (ppm)
    uint16_t TVOC;              // TVOC 浓度 (ppb)
    uint16_t baselineCO2;       // CO₂ 基线值
    uint16_t baselineTVOC;      // TVOC 基线值
    uint16_t featureSetVersion; // 特性集版本
    uint16_t H2;                // H₂ 信号
    uint16_t ethanol;           // 乙醇信号
    uint64_t serialID;          // 64位序列号
} SGP30_HandleTypeDef;

// 命令定义
extern const uint8_t sgp30_init_air_quality[2];
extern const uint8_t sgp30_measure_air_quality[2];
extern const uint8_t sgp30_get_baseline[2];
extern const uint8_t sgp30_set_baseline[2];
extern const uint8_t sgp30_set_humidity[2];
extern const uint8_t sgp30_measure_test[2];
extern const uint8_t sgp30_get_feature_set_version[2];
extern const uint8_t sgp30_get_serial_id[2];
extern const uint8_t sgp30_measure_raw_signals[2];

/**
 * @brief CRC8 计算函数
 * @param data 16位数据
 * @return 8位CRC值
 */
uint8_t SGP30_CRC8(uint16_t data);

/**
 * @brief 初始化传感器
 * @param hsgp SGP30 句柄
 * @param i2cInstance I2C 实例号
 * @param i2cState I2C 状态指针
 * @return SGP30_Status 初始化状态
 */
SGP30_Status SGP30_Init(SGP30_HandleTypeDef *hsgp, uint8_t i2cInstance, i2c_master_state_t *i2cState);

/**
 * @brief 初始化空气质量测量
 * @param hsgp SGP30 句柄
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_InitAirQuality(SGP30_HandleTypeDef *hsgp);

/**
 * @brief 测量空气质量
 * @param hsgp SGP30 句柄
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_MeasureAirQuality(SGP30_HandleTypeDef *hsgp);

/**
 * @brief 获取基线值
 * @param hsgp SGP30 句柄
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_GetBaseline(SGP30_HandleTypeDef *hsgp);

/**
 * @brief 设置基线值
 * @param hsgp SGP30 句柄
 * @param baselineCO2 CO₂ 基线值
 * @param baselineTVOC TVOC 基线值
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_SetBaseline(SGP30_HandleTypeDef *hsgp, uint16_t baselineCO2, uint16_t baselineTVOC);

/**
 * @brief 设置湿度补偿
 * @param hsgp SGP30 句柄
 * @param humidity 湿度值 (g/m³)
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_SetHumidity(SGP30_HandleTypeDef *hsgp, uint16_t humidity);

/**
 * @brief 获取特性集版本
 * @param hsgp SGP30 句柄
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_GetFeatureSetVersion(SGP30_HandleTypeDef *hsgp);

/**
 * @brief 测量原始信号
 * @param hsgp SGP30 句柄
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_MeasureRawSignals(SGP30_HandleTypeDef *hsgp);

/**
 * @brief 通用调用复位
 * @param i2cInstance I2C 实例号
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_GeneralCallReset(uint8_t i2cInstance);

/**
 * @brief 获取序列号
 * @param hsgp SGP30 句柄
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_GetSerialID(SGP30_HandleTypeDef *hsgp);

/**
 * @brief 运行自检
 * @param hsgp SGP30 句柄
 * @return SGP30_Status 操作状态
 */
SGP30_Status SGP30_MeasureTest(SGP30_HandleTypeDef *hsgp);

#endif // SGP30_ESWIN_H

#ifdef EXAMPLE
#include "sgp30_eswin.h"

// I2C 实例和状态
i2c_master_state_t g_stI2C0MasterState;
#define I2C_INSTANCE INST_I2C_0

int main(void)
{
    // 初始化系统时钟
    CLOCK_SYS_Init(g_pstClockManConfigsArr[0]);

    // 初始化 I2C
    I2C_DRV_MasterInit(I2C_INSTANCE, &g_stI2c0MasterUserConfig0, &g_stI2C0MasterState);

    // 初始化 SGP30
    SGP30_HandleTypeDef sgp30;
    SGP30_Status status = SGP30_Init(&sgp30, I2C_INSTANCE, &g_stI2C0MasterState);

    if (status != SGP30_SUCCESS) {
        printf("SGP30 initialization failed: %d", status);
        return -1;
    }

    printf("SGP30 Serial ID: 0x%016llX", sgp30.serialID);

    // 初始化空气质量测量
    status = SGP30_InitAirQuality(&sgp30);
    if (status != SGP30_SUCCESS) {
        printf("Air quality init failed: %d", status);
        return -1;
    }

    while (1) {
        // 测量空气质量
        status = SGP30_MeasureAirQuality(&sgp30);
        if (status == SGP30_SUCCESS) {
            printf("CO₂: %d ppm, TVOC: %d ppb", sgp30.CO2, sgp30.TVOC);
        } else {
            printf("Measurement failed: %d", status);
        }

        OS_DelayMs(1000); // 每秒测量一次
    }
}

#endif
